package com.abu.apply.controller;

import cn.hutool.core.util.StrUtil;
import com.abu.apply.client.UserClient;
import com.abu.apply.config.mvc.LoginInterceptor;
import com.abu.apply.service.ApplyReportService;
import com.abu.apply.vo.ApplyStatusCountVO;
import com.abu.apply.vo.QueryConditionSelect;
import com.abu.apply.vo.UserInfo;
import com.abu.common.Resp;
import com.abu.apply.pojo.ApplyReport;
import com.abu.apply.vo.QueryApplyReportVo;

import com.abu.course.vo.ClassroomOrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 申请记录表 前端控制器
 * </p>
 *
 * @author 阿布
 * @since 2021-02-03
 */
@RestController
@RequestMapping("/apply")
public class ApplyReportController {

    @Autowired
    private ApplyReportService applyReportService;

    @GetMapping("/userinfo")
    public Resp getLoginUserInfo() {
        UserInfo userInfo = LoginInterceptor.getUserInfo();
        return Resp.ok().data("userInfo", userInfo);
    }

    @GetMapping("/queryAllStatusCount")
    public Resp getStatusCount() {
        List<ApplyStatusCountVO> applyStatusCountVOList = this.applyReportService.selectAllCount();
        if (CollectionUtils.isEmpty(applyStatusCountVOList)) {
            return Resp.error().msg("admin，请选择预定模块查看");
        }
        return Resp.ok().data("applyStatusCountVOList", applyStatusCountVOList);
    }

    @GetMapping("noAuthorize/all-apply-course-history")
    public Resp queryAllHistory(@RequestParam("classroomName") String classroomName) {
        List<ApplyReport> classroomOrderVOList = this.applyReportService.queryCourseAndApplyHistory(classroomName);
        if (CollectionUtils.isEmpty(classroomOrderVOList)) {
            return Resp.error().msg("数据为空");
        }
        return Resp.ok().data("pageResult", classroomOrderVOList);
    }


    @GetMapping("noAuthorize/all-history")
    public Resp queryAllHistory(QueryConditionSelect queryConditionSelect) {
        HashMap<String, Object> mapResult = this.applyReportService.queryPageByCondition(queryConditionSelect);
        return Resp.ok().data("pageResult", mapResult);
    }

    @GetMapping("noAuthorize/history")
    public Resp queryPageByCondition(QueryConditionSelect queryConditionSelect) {
        HashMap<String, Object> mapResult = this.applyReportService.selectByPage(queryConditionSelect);
        return Resp.ok().data("pageResult", mapResult);
    }

    @GetMapping("/list/apply/userId")
    public Resp queryApplyListByUserId(
            @RequestParam(value = "page", defaultValue = "1") Long page,
            @RequestParam(value = "limit", defaultValue = "10") Long limit,
            @RequestParam(value = "key", required = false) String key
    ) {
        HashMap<String, Object> mapResultData = this.applyReportService.queryApplyListByUserId(page, limit, key);
        return Resp.ok().data("mapResultData", mapResultData);
    }

    @GetMapping("applylist")
    public Resp getAllApplicationsByCondition(QueryApplyReportVo queryApplyReportVo) {
        HashMap<String, Object> mapResult = this.applyReportService.queryApplyByPage(queryApplyReportVo);
        return Resp.ok().data(mapResult);
    }

    @GetMapping("noAuthorize/one")
    public Resp getApplyReportByApplyId(@RequestParam("id") Long applyId) {
        ApplyReport applyReport = this.applyReportService.getById(applyId);
        return Resp.ok().data("applyReport", applyReport);
    }

    @GetMapping("noAuthorize/count")
    public Resp queryNonInspectApplyReport(@RequestParam("reviewerId") Long reviewerId,
                                           @RequestParam(value = "status") Integer status) {
        Integer count = this.applyReportService.queryNonInspectApplyReport(reviewerId, status);
        return Resp.ok().data("count", count);
    }

    @GetMapping("noAuthorize/type")
    public Resp getAllType() {
        HashMap<String, Object> mapResult = this.applyReportService.getTypes();
        return Resp.ok().data(mapResult);
    }

    @PutMapping("data/all")
    public Resp updateApplyByApplyId(@RequestBody ApplyReport applyReport) {
        applyReport.setApplyStatus(0);
        boolean flag = this.applyReportService.updateById(applyReport);
        if (flag) {
            return Resp.ok().msg("修改成功");
        }
        return Resp.error().msg("修改失败");
    }

    @PostMapping("saveApplyReport")
    public Resp saveApply(@RequestBody ApplyReport reviewForm) {
        Long userId = LoginInterceptor.getUserInfo().getUserId();
        reviewForm.setUserId(userId);
        boolean flag = this.applyReportService.save(reviewForm);
        if (flag) {
            return Resp.ok().msg("修改成功");
        }
        return Resp.error().msg("添加失败");
    }


    @PutMapping("apply-id/{id}")
    public Resp updateApplyState(@PathVariable("id") Long applyId, @RequestParam("status") Integer status) {
        boolean flag = this.applyReportService.updateState(applyId, status);
        if (flag) {
            return Resp.ok().msg("操作成功");
        }
        return Resp.error().msg("操作失败");
    }

    @DeleteMapping("apply-id/{id}")
    public Resp deleteByApplyId(@PathVariable("id") Long applyId) {
        boolean flag = this.applyReportService.removeById(applyId);
        if (flag) {
            return Resp.ok().msg("删除成功");
        }
        return Resp.error().msg("删除失败");
    }

    @DeleteMapping("apply-ids")
    public Resp deleteByApplyId(String idStr) {
//        idStr="1,2,3"
        String[] idArray = StrUtil.split(idStr, ",");
        List<String> idList = Arrays.asList(idArray);
        boolean flag = this.applyReportService.removeByIds(idList);
        if (flag) {
            return Resp.ok().msg("删除成功");
        }
        return Resp.error().msg("删除失败");
    }

    @DeleteMapping("ids")
    public Resp batchDeleteById(@RequestParam("applyIdList") List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Resp.error().msg("未选中数据").code(400);
        }
        boolean flag = this.applyReportService.removeByIds(ids);
        if (flag) {
            return Resp.ok().msg("批量删除成功");
        }
        return Resp.error().msg("批量删除失败");
    }
}

